package com.neiquan.backstage.user.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.pub.bsc.dao.po.User;
import com.neiquan.backstage.user.UserService;

@Service
public class UserServiceImpl implements UserService {
	private static Class<?> poc = User.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;

	@Override
	public User queryByPhone(String phone, Integer memberId) {
		DetachedCriteria detached = DetachedCriteria.forClass(poc);
		detached.add(Restrictions.eq("phone", phone));
		detached.add(Restrictions.eq("memberId", memberId));
		return extraSpringHibernateTemplate.findFirstOneByCriteria(detached);
	}
	
	@Override
	public User queryByName(String name, Integer memberId) {
		DetachedCriteria detached = DetachedCriteria.forClass(poc);
		detached.add(Restrictions.eq("name", name));
		detached.add(Restrictions.eq("memberId", memberId));
		return extraSpringHibernateTemplate.findFirstOneByCriteria(detached);
	}
	
	@Override
	public User queryById(Integer id) {
		return extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userId", id);
	}

	@Override
	public List<User>queryByentreprEneurName(String entrepreneurName, int memberId) {
		// TODO Auto-generated method stub
		return (List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where entrepreneurName=? and memberId=? and type!=1",entrepreneurName, memberId);
	}

	@Override
	public User queryByopendId(String openId, Integer memberId) {
		// TODO Auto-generated method stub
		List<User> users=(List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where opendId=? and memberId=?", openId,memberId);
		if(users.size()>0){
			return users.get(0);
		}
			return null;	
	}
	
}
